<?php
/**
 * Created Dec. 7, 2009
 * @author : Christopher Sheldahl
 */
 
/**
 * LrpParams : parameters needed to write an .lrp file
 * 
 */
 class LrpParams {
 		//the variables here are private so as to avoid checks on their values
 		//in functions where they are used.
 		private $workingDirectory;  //string
 		private $transmitterFileName; //if file is "foo.lrp"", 
 									//$transmitterFileName == "foo" - also used
 									//for radiation pattern files, dcf color 
 									//definition files and qth files
 		private $dielectric; //float
 		private $conductivity;  //siemens/meter (float)
 		private $bending; //atmospheric bending constant in N-units (float)
 		private $freq; //frequency in Hz (20 MHz to 20 GHz) (float)
 		private $climate; //integer 1<=$climate <= 7
 		private $polar;  //polarization (0=horizontal; 1 = vertical) (integer)
 		private $situation;  //situation fraction (float) 0.0 <= $situation <= 1.0
 		private $time; //fraction of time (float) 0.0 <= $time <= 1.0
 		private $erp; //ERP in Watts (float) (set to 0 if not specified
 		function __construct($workingDirectory, $transmitterFileName, $freq, 
 						$climate, $polar, $erp = 0, $situation = 0.50, 
 						$time = 0.90, $dielectric = 15, $conductivity = 0.005,
 						$bending = 301)
 		{
 			if (($freq < 20) or ($freq > 20000))
 			{
 				throw new Exception("Frequency should be in range from 20 to 20000 MHz");
 			}
 			if (($climate < 1) or ($climate > 7) or (! is_int($climate) ) )
 			{
 				throw new Exception(
 				"climate code should be an integer between 1 and 7 inclusive");
 			}
 			if (($polar < 0) or ($polar > 1) or ( ! is_int( $polar) ) )
 			{
 				throw new Exception("polarization must be 0 (horizontal) or 1 (vertical)");
 			}
 			if (($situation < 0.0) or ($situation > 1.0))
 			{
 				throw new Exception(
 				"situation fraction must be a number between 0.0 and 1.0 inclusive"
 				);
 			}
 			if (($time < 0.0) or ($time > 1.0))
 			{
 				throw new Exception(
 				"time fraction must be a number between 0.0 and 1.0 inclusive"
 				);
 			}
 			if ($erp < 0)
 			{
 				throw new Exception("erp must be nonnegative");
 			}
 			
 			$this->workingDirectory = $workingDirectory;
 			$this->transmitterFileName = $transmitterFileName;
 			$this->freq = $freq;
 			$this->climate = $climate;
 			$this->polar = $polar;
 			$this->erp = $erp;
 			$this->situation = $situation;
 			$this->time = $time;
 			$this->dielectric = $dielectric;
 			$this->conductivity = $conductivity;
 			$this->bending = $bending;
	}  //close constructor
	
	
	function getDir() {
		return $this->workingDirectory;
	}
	
	function getFileName() {
		return $this->transmitterFileName;
	}
	
	function getFreq() {
		return $this->freq;
	}
	
	
	function getClimate() {
		return $this->climate;
	}
	
	function getPolar() {
		return $this->polar;
	}
	
	function getErp() {
		return $this->erp;
	}
	
	function getSituation() {
		return $this->situation;
	}
	
	function getTime() {
		return $this->time;
	}	
	
	function getDielectric() {
		return $this->dielectric;
	}	
	
	function getConductivity() {
		return $this->conductivity;
	}	
	
	
	function getBending() {
		return $this->bending;
	}	
	
 } 
 
 class WriteLrpUtil {
 		/** 
 		 * $params : a LrpParams 
 		 * Writes an .lrp file with name $params->transmitterFileName . ".lrp"
 		 * to directory $params->workingDirectory
 		 * 
 		 */
 		function writeFile( $params ) {
 			$workingDirectory = $params->getDir();
 			$errorOccurred = false;
 			if ( ! is_readable($workingDirectory) )
			{
				$exceptionMessage = $exceptionMessage . $workingDirectory . 
				" is not readable. ";
				$errorOccurred = true;
			}
			if ( ! is_writable($workingDirectory) )
			{
				$exceptionMessage = $exceptionMessage . $workingDirectory . 
					" is not writable. ";
				$errorOccurred = true;
			}
			/* Commented by Tanner 12/8 ##################################
			if ( ! is_executable($workingDirectory) )
			{
				$exceptionMessage = $exceptionMessage . $workingDirectory .
					" is not executable. ";
				$errorOccurred = true;
			}
			*/
			if ( $errorOccurred )
			{
				throw new Exception( $exceptionMessage );
			}
 			chdir($workingDirectory);
 			$fileName = $params->getFileName() . ".lrp";
 			$file = fopen($fileName,"w") or die("Can't open file.");
 			fwrite($file, $params->getDielectric() . "\n");
 			fwrite($file, $params->getConductivity(). "\n");
 			fwrite($file, $params->getBending() . "\n");
 			fwrite($file, $params->getFreq() . "\n");
 			fwrite($file, $params->getClimate() . "\n");
 			fwrite($file, $params->getPolar() . "\n");
 			fwrite($file, $params->getSituation() . "\n");
 			fwrite($file, $params->getTime() . "\n");
 			$erp = $params->getErp();
 			if ( $erp > 0 )
 			{
 				fwrite($file, $erp . "\n");
 			}
 			fwrite($file, "\n\n");
 			fclose($file);
 		} //close writeFile function
 			
 }  //close class  
 //MAIN
 /*
 echo("Testing...");
 $dir = "/Users/sheldahl/splat-1.3.0";
 $fileName = "test";
 $freq = 605;
 $climate = 5;
 $polar = 0;
 $test = new LrpParams( $dir, $fileName, $freq, $climate, $polar);
 WriteLrpUtil::writeFile( $test );
 echo("\nDone.\n");
 */
 ?>
 
 